//@version: $Id: main.cpp 64 2010-11-25 20:46:39Z NeurobaticsTim@gmail.com $

#include "Global.h"

#ifdef _STANDALONE_MAP_H

#include "Map.h"

#include <stdio.h>
#include <conio.h>

/*Author: Jared Bills*/
//Pauses the console and waits for input
inline void pause() {
	printf("Press any key to continue . . . ");
	_getch();
	putchar('\n');
}

int main(int argc, char ** argv) {
	Map testmap;
	testmap.init();
	UserInput blah, hi;
	unsigned int input[4] = {0, 0, 0, 0};
	testmap.draw();
	do {
		printf("Type in coordinates for desired swap (x, y), (x, y): ");
		for(unsigned int uiI = 0; uiI < 4; ++uiI) {
			input[uiI] = _getch() - '0';
			printf("%d ", input[uiI]);
		}
		putchar('\n');
		blah.mouseGridLocation.x = input[0]; blah.mouseGridLocation.y = input[1];
		hi.mouseGridLocation.x = input[2]; hi.mouseGridLocation.y = input[3];
		testmap.handleInput(blah);
		testmap.handleInput(hi);
		if(testmap.comboRenew())
			printf("Combos found!\n");
		else
			printf("No combos found!\n");
		testmap.draw();
	} while(testmap.check());
	printf("End Game.\n");
	pause();
	return 0;
}
#else

#include <windows.h>
#include <stdio.h>
#include "Renderer.h"

//global Variables
HWND wndHandle; 
bool g_bIsVisible = true;	// Don't draw if can't be seen


bool initWindow(HINSTANCE hInstance);//initializes window

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //handles windows messages
void Draw()//stubbed
{
	MessageBox(NULL, "Draw Stub", "HERE", MB_OK);
}
void HandleInput()//stubbed
{
	MessageBox(NULL,"INPUT STUFF","HERE", MB_OK);
}
bool gameover() //stubbed
{
	return true;
}
void update(bool &acceptable)//stubbed
{
	HandleInput();
	if(!gameover())
		acceptable = false;
	else
		acceptable = true;
}
bool initmap()//stubbed
{
	MessageBox(NULL,"MAP STUB","IT WORKS",MB_OK);
	return true;
}
bool initgame()//initializes renderer and map
{
	Renderer::Instance()->InitializeD3D(wndHandle);
	Renderer::Instance()->InitializeDemo();

	if(initmap())
		return true;
	else
		return false;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	bool running = true;
	//make a window
	if(!initWindow(hInstance))
	{
		//if it didnt make it, display this message
		MessageBox(NULL, "Unable to create window", "ERROR" , MB_OK);
		return false;
	}
	//initialize directx
	if(!initgame())
	{
		MessageBox(NULL, "Unable to initialize map", "ERROR", MB_OK);
		return false;
	}

	MSG msg;
	ZeroMemory(&msg, sizeof(msg));

	//message loop
	while(msg.message != WM_QUIT && running == true)
	{
		if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))//0U = zero
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			//if there arent any messages, it should draw things
			//dxMgr.beginRender();//placeholder
			Draw();
			update(running);
			//dxMgr.endRender();//placeholder
		}
	}

	//shutdown the directx manager
	//dxMgr.shutdown();
	return (int) msg.wParam;
}//end of main

bool initWindow(HINSTANCE hInstance)
{
	//http://books.google.com/books?id=n0hcRvDniVQC&pg=PA13&lpg=PA13&dq=directx+wndclassex&source=bl&ots=hIKjv-wcHr&sig=tVApXVNQIr8T5Te9QYne8UJXStg&hl=en&ei=uKTkTL3iDYj0tgOZ4v3oCA&sa=X&oi=book_result&ct=result&resnum=4&sqi=2&ved=0CDcQ6AEwAw#v=onepage&q&f=false
	//Beginning Directx 9 by Wendy Jones
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);//size of the structure
	wcex.style = CS_HREDRAW | CS_VREDRAW;//class style
	wcex.lpfnWndProc = (WNDPROC)WndProc;//***//window procedure call back
	wcex.cbClsExtra = 0;//extra bytes to allocate for this class
	wcex.cbWndExtra = 0;//extra bytes to allocate for this instance
	wcex.hInstance = hInstance;//handle to the application instance
	wcex.hIcon = 0;//icon for application
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);//default cursor
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);//background color
	wcex.lpszMenuName = NULL;//resource name for the menu
	wcex.lpszClassName = "WindowsApp";//class name
	wcex.hIconSm = 0;//handle for the small cion
	RegisterClassEx(&wcex);

	wndHandle = CreateWindow("WindowsApp", //class name
							 "Windows Program", //windowName
							 WS_OVERLAPPEDWINDOW, //style//windowed
							 //WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,//fullscreen
							 CW_USEDEFAULT, //x
							 CW_USEDEFAULT, //y
							 640,//1440, //height//640
							 480,//900, //width//480
							 NULL, //parent
							 NULL, //menu
							 hInstance,
							 NULL);//param
	
	if(!wndHandle)
		return false;

	ShowWindow(wndHandle, SW_SHOW);
	UpdateWindow(wndHandle);

	return true;
}
//handles messages
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_DESTROY:
		case WM_CLOSE:
		PostQuitMessage(0);
		break;

		case WM_ACTIVATE:
			{
				WORD wAction = LOWORD(wParam);
				bool bIsMinimized = (HIWORD(wParam) != 0);
				g_bIsVisible = ! bIsMinimized;
			}
			return 0;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

#endif